'************************************************************************************
' Copyright  2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
'  Summary: Tools to facilitate working with files.

Option Strict On
Option Explicit On 

Imports system

Namespace KADGen.Utility

   Public Class FileTools
      ' CHANGED: KAD 20041026 Removed the contents of this file. Did not delete because this may be part of the Project stuff I am reinstating
      ' Added back: KAD 20050316
      Private Const fileListXmlNamespace As String = "http://kadgen/filelist.xsd"

      Public Shared Function GetFileListXML( _
                  ByVal startDir As String) _
                  As IO.Stream
         Dim xmlDoc As New Xml.XmlDocument
         Dim node As Xml.XmlNode
         Dim dir As New IO.DirectoryInfo(startDir)
         Dim stream As New IO.MemoryStream
         xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes"))
         'node = xmlDoc.CreateElement("fil:FileLists", fileListXmlNamespace)
         'xmlDoc.AppendChild(node)
         node = xmlDoc.AppendChild(xmlHelpers.NewElement("fil", _
                        fileListXmlNamespace, _
                        xmlDoc, "FileLists"))
         node = node.AppendChild(xmlHelpers.NewElement( _
                        fileListXmlNamespace, _
                        xmlDoc, "fil:FileList"))
         node.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, "StartDir", _
                     startDir))
         node.AppendChild(FileListXML(dir, xmlDoc))
         xmlDoc.Save(stream)
         Return stream
      End Function

      Private Shared Function FileListXML( _
                  ByVal dir As IO.DirectoryInfo, _
                  ByVal xmlDoc As Xml.XmlDocument) _
                  As Xml.XmlNode
         Dim node As Xml.XmlNode = xmlHelpers.NewElement( _
                        fileListXmlNamespace, _
                        xmlDoc, "fil:Dir", dir.Name)
         node.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, "Name", _
                     dir.Name))
         Dim dirs As IO.DirectoryInfo()
         Dim files As IO.FileInfo()
         node.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "FullName", dir.FullName))
         dirs = dir.GetDirectories
         files = dir.GetFiles()

         For Each d As IO.DirectoryInfo In dirs
            node.AppendChild(FileListXML(d, xmlDoc))
         Next

         For Each f As IO.FileInfo In files
            node.AppendChild(FileInfoXML(xmlDoc, f))
         Next
         Return node
      End Function

      Private Shared Function FileInfoXML( _
                  ByVal xmlDoc As Xml.XmlDocument, _
                  ByVal f As IO.FileInfo) _
                  As Xml.XmlNode
         Dim child As Xml.XmlNode
         child = xmlHelpers.NewElement( _
                        fileListXmlNamespace, _
                        xmlDoc, "fil:File", f.Name)
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "Ext", f.Extension))
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "FullName", f.FullName))
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "Attributes", f.Attributes.ToString))
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "CreationUTC", f.CreationTimeUtc.ToString))
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "LastWriteUTC", f.LastWriteTimeUtc.ToString))
         child.Attributes.Append(xmlHelpers.NewAttribute(xmlDoc, _
                      "Length", f.Length.ToString))
         Return child
      End Function

      Public Shared Sub ForceFileWritable(ByVal fileName As String)
         IO.File.SetAttributes(fileName, IO.File.GetAttributes(fileName) And Not IO.FileAttributes.ReadOnly)
      End Sub

      Public Shared Sub ForceFileReadOnly(ByVal fileName As String)
         IO.File.SetAttributes(fileName, IO.File.GetAttributes(fileName) Or IO.FileAttributes.ReadOnly)
      End Sub

      Public Shared Sub FixWhiteSpace( _
               ByVal fileName As String, _
               ByVal UseTabs As Boolean, _
               ByVal spaceSize As Int32)
         Dim reader As IO.StreamReader
         Dim writer As IO.StreamWriter
         Dim line As String
         Dim tempLine As String
         Dim iIndent As Int32
         Dim sb As New Text.StringBuilder

         reader = New IO.StreamReader(fileName)
         line = reader.ReadLine
         Do While Not line Is Nothing
            line = reader.ReadLine.Trim
            tempLine = RemoveAdjectives(line)
            iIndent += IndentWord(tempLine)
            If line.Length = 0 Then
               sb.Append(Tools.vbcrlf)
            Else
               sb.Append(indent(iIndent, UseTabs, spaceSize) & line)
            End If
            iIndent -= OutdentWord(tempLine)
         Loop
         reader.Close()
         writer = New IO.StreamWriter(fileName)

      End Sub

      Private Shared Function RemoveAdjectives( _
               ByVal line As String) _
               As String
         Dim ret As String
         ret = line
         ret = ret.Replace("Public", "")
         ret = ret.Replace("Private", "")
         ret = ret.Replace("Protected", "")
         ret = ret.Replace("Friend", "")
         ret = ret.Replace("Overloads", "")
         ret = ret.Replace("Overrides", "")
         ret = ret.Replace("Overridable", "")
         ret = ret.Replace("MustOverride", "")
         ret = ret.Replace("Shared", "")
         ret = ret.Replace("Shadows", "")
         Return ret
      End Function

      Private Shared Function IndentWord( _
               ByVal line As String) _
               As Int32
         Dim words() As String
         words = line.Split(" "c)
         Select Case words(0).ToLower
            Case "namespace"
               Return 1
            Case "sub"
               Return 1
            Case "function"
               Return 1
            Case "property"
               Return 1
            Case "get"
               Return 1
            Case "set"
               Return 1
            Case "if"
               Return 1
            Case "select"
               Return 1
            Case "case"
               Return 1
            Case "for"
               Return 1
            Case "do"
               Return 1
            Case "while"
               Return 1
            Case Else
               Return 0
         End Select
      End Function

      Private Shared Function OutdentWord( _
               ByVal line As String) _
               As Int32
         Dim lowerLine As String
         lowerLine = line.ToLower
         If lowerLine.StartsWith("end namespace") Then
            Return 1
         ElseIf lowerLine.StartsWith("end sub") Then
            Return 1
         ElseIf lowerLine.StartsWith("end function") Then
            Return 1
         ElseIf lowerLine.StartsWith("end property") Then
            Return 1
         ElseIf lowerLine.StartsWith("end get") Then
            Return 1
         ElseIf lowerLine.StartsWith("end set") Then
            Return 1
         ElseIf lowerLine.StartsWith("end if") Then
            Return 1
         ElseIf lowerLine.StartsWith("end select") Then
            Return 2
         ElseIf lowerLine.StartsWith("next") Then
            Return 1
         ElseIf lowerLine.StartsWith("loop") Then
            Return 1
         ElseIf lowerLine.StartsWith("end while") Then
            Return 1
         Else
            Return 0
         End If

      End Function

      Private Shared Function Indent( _
               ByVal indentLevel As Int32, _
               ByVal useTabs As Boolean, _
               ByVal spaceSize As Int32) _
               As String
         If useTabs Then
            Return New String(Microsoft.VisualBasic.ControlChars.Tab, indentLevel)
         Else
            Return New String(" "c, indentLevel * spaceSize)
         End If
      End Function
   End Class
End Namespace